<?php

/**
 * product actions.
 *
 * @package    sf_sandbox
 * @subpackage product
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class productActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->products = Doctrine_Core::getTable('Product')
      ->createQuery('a')
      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ProductForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ProductForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    error_reporting(E_ALL);
    ini_set('display_errors', 0);   
    $this->forward404Unless($product = Doctrine_Core::getTable('Product')->find(array($request->getParameter('id_product'))), sprintf('Object product does not exist (%s).', $request->getParameter('id_product')));
    $this->form = new ProductForm($product);
    // Obtiene la lista de los bullet points
    $this->bullet_point_products = Doctrine_Core::getTable('BulletPointProduct')
      ->createQuery('a')
      ->where('a.id_product = ?', $request->getParameter('id_product'))
      ->execute();
    // Obtiene las imagenes de la galeria para el producto
    $this->album_products = Doctrine_Core::getTable('AlbumProduct')
      ->createQuery('b')
      ->where('b.id_product = ?', $request->getParameter('id_product'))
      ->execute();
    
    $this->getUser()->setAttribute('id_product', $request->getParameter('id_product'));
    
  }
  
  public function executeUploadImage(sfWebRequest $request)
  {
    
    $this->setLayout('layoutErrorImage');
    sfProjectConfiguration::getActive()->loadHelpers('uploadFile');
    // Subo la imagen    
    $this->errorImage = false;
    if($_FILES['uploadfile']['size'] >  sfConfig::get('app_image_size_max'))
    {
        $this->errorImage = true;
    }else{
        $this->errorImage = false;

        $idProduct = $this->getUser()->getAttribute('id_product');
        $idSeller = $this->getUser()->getAttribute('id_user_seller');
        // Guardo los datos de la imagen
        $this->item = new AlbumProduct();
        $this->item->setIdProduct($idProduct);        
        $this->item->setStatus(1);
        $this->item->save();
        // Ahora carga la imagen en el directorio
        $this->fileUploaded = loadFiles($_FILES['uploadfile']['name'], $_FILES['uploadfile']['tmp_name'], 0, sfConfig::get('sf_upload_dir').'/products/'.$idSeller.'/gallery/', $idProduct, false);
        $this->item->setImage($this->fileUploaded);
        $this->item->save();
    }
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($product = Doctrine_Core::getTable('Product')->find(array($request->getParameter('id_product'))), sprintf('Object product does not exist (%s).', $request->getParameter('id_product')));
    $this->form = new ProductForm($product);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($product = Doctrine_Core::getTable('Product')->find(array($request->getParameter('id_product'))), sprintf('Object product does not exist (%s).', $request->getParameter('id_product')));
    $product->delete();

    $this->redirect('product/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $product = $form->save();
      // Si hay imagen
      if($product->getImage()){
        $dir  = sfConfig::get('sf_upload_dir')."/products/".$product->getIdUser()."/";
        $archivo = $product->getImage();
        $archivoBig = "big_".$product->getImage();
        $archivoMed = "medium_".$product->getImage();
        $archivoSmall = "small_".$product->getImage();
        
        $thumbnail = new sfThumbnail('500', '450',true,false);
        $thumbnail->loadFile($dir.$archivo);
        $thumbnail->save($dir.$archivo);
        $thumbnail = new sfThumbnail('400', '300',true,false);
        $thumbnail->loadFile($dir.$archivo);
        $thumbnail->save($dir.$archivoMed);
        $thumbnail = new sfThumbnail('130', '97',true,false);
        $thumbnail->loadFile($dir.$archivo);
        $thumbnail->save($dir.$archivoSmall);
        
      }
      
      $this->redirect('product/edit?id_product='.$product->getIdProduct());
    }
  }
  
  public function executeDeleteImage(sfWebRequest $request)
  {
    $this->forward404Unless($Model = Doctrine::getTable('Product')->find($request->getParameter('id')), sprintf('Object Model does not exist (%s).', $request->getParameter('id')));
    
    //Delete images process
    if ($Model->getImage())
    {
      $appYml = sfConfig::get('app_upload_images_product');
      $uploadDir = sfConfig::get('sf_upload_dir').'/products/'.$Model->getIdUser().'/';
      for($i=1;$i<=$appYml['copies'];$i++)
      {
        //Delete images from uploads directory
        if(is_file($uploadDir.$appYml['size_'.$i]['pref_'.$i].'_'.$Model->getImage()))
        {
          unlink($uploadDir.$appYml['size_'.$i]['pref_'.$i].'_'.$Model->getImage());
        }
      }
      unlink($uploadDir.$Model->getImage());
    }
    $Model->setImage('');
    $Model->save();
  }
  
  public function executeDeleteImageGallery(sfWebRequest $request)
  {
    $this->forward404Unless($Model = Doctrine::getTable('AlbumProduct')->find($request->getParameter('id')), sprintf('Object Model does not exist (%s).', $request->getParameter('id')));
    
    //Delete images process
    if ($Model->getImage())
    {
      $appYml = sfConfig::get('app_upload_images_product');
      $uploadDir = sfConfig::get('sf_upload_dir').'/products/'.$this->getUser()->getAttribute('id_user_seller').'/gallery/';
      for($i=1;$i<=$appYml['copies'];$i++)
      {
        //Delete images from uploads directory
        if(is_file($uploadDir.$appYml['size_'.$i]['pref_'.$i].'_'.$Model->getImage()))
        {
          unlink($uploadDir.$appYml['size_'.$i]['pref_'.$i].'_'.$Model->getImage());
        }
      }
      unlink($uploadDir.$Model->getImage());
    }
    $Model->delete();
    return true;
  }
  
  public function executeListBulletPoint(sfWebRequest $request)
  {
      $this->forward404Unless($Model = Doctrine::getTable('Product')->find($request->getParameter('id')), sprintf('Objectaaaa Model does not exist (%s).', $request->getParameter('id')));
      $this->bullet_point_products = Doctrine_Core::getTable('BulletPointProduct')
      ->createQuery('a')
      ->where('a.id_product = ?', $request->getParameter('id'))
      ->execute();
  }
  
  public function executeSaveBulletPoint(sfWebRequest $request)
  {
      $this->forward404Unless($Model = Doctrine::getTable('Product')->find($request->getParameter('id')), sprintf('Object Model does not exist (%s).', $request->getParameter('id')));
      $bullet = new BulletPointProduct();
      $bullet->id_product = $request->getParameter('id');
      $bullet->bullet_point_descripcion = $request->getParameter('name_bullet');      
      $bullet->save();
  }
          
}
